package com.br.betelgeuse.bean.filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.br.betelgeuse.bean.UsuarioInternoBean;

@WebFilter(filterName="LoginUsuarioInternoFilter", urlPatterns="/modulos/*")
public class LoginUsuarioInternoFilter implements Filter {

	public LoginUsuarioInternoFilter() {
		// TODO Auto-generated constructor stub
	}

	public void init(FilterConfig arg0) throws ServletException {

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;
		UsuarioInternoBean session = (UsuarioInternoBean) req.getSession()
				.getAttribute("usuarioInternoBean");

		if (session != null && session.isLogged()) {
			chain.doFilter(request, response);
		} else {
			resp.sendRedirect(req.getServletContext().getContextPath()
					+ "/login.jsf");
		}
	}

	public void destroy() {

	}

}
